/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.tests;

import static ac.jp.u_tokyo.SyncLib.util.Helper.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ac.jp.u_tokyo.SyncLib.IDSync;
import ac.jp.u_tokyo.SyncLib.Mod;
import ac.jp.u_tokyo.SyncLib.NullMod;
import ac.jp.u_tokyo.SyncLib.Sync;
import ac.jp.u_tokyo.SyncLib.SynchronizationFailedException;

public class IDSyncTest extends SyncTestBase
{
	
	Sync _sync;
	Sync _vsync;
	
	protected Sync createForwardSync() throws SynchronizationFailedException {
		IDSync sync = new IDSync(true);
		return sync;
	}
	
	protected Sync createBackwardSync() throws SynchronizationFailedException {
		IDSync sync = new IDSync(false);
		return sync;
	}
	
	@Before
	public void setUp() throws SynchronizationFailedException {
		_sync = createForwardSync();
		_vsync = createBackwardSync();
		_sync.resynchronize(new Object[] {null, null}, new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE});
		_vsync.resynchronize(new Object[] {null, null}, new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE});
		
	}
	
	@Test
	public void testSyncNulls() throws SynchronizationFailedException {
		final Mod[] nulls = new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE};
		final Mod[] result = checkedSynchronize(_sync, nulls);
		assertArrayEquals(nulls, result);
	}

	@Test
	public void testResyncNulls() throws SynchronizationFailedException {
		final Mod[] nullMods = new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE};
		final Object[] nulls = new Object[] {null, null};
		final Mod[] result = checkedResynchronize(_sync, nulls, nullMods);
		assertArrayEquals(nullMods, result);
	}

	@Test
	public void testResync1to2() throws SynchronizationFailedException {
		final Mod[] nullMods = new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE};
		Object[] values = new Object[] {1, 2};
		final Mod[] result = checkedResynchronize(_sync, values, nullMods);
		assertArrayEquals(createPrimitiveModList(1, 1), result);
	}

	
	@Test
	public void testResync2to1() throws SynchronizationFailedException {
		final Mod[] nullMods = new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE};
		Object[] values = new Object[] {1, 2};
		final Mod[] result = checkedResynchronize(_vsync, values, nullMods);
		assertArrayEquals(createPrimitiveModList(2, 2), result);
	}

	@Test
	public void testKeepMod() throws SynchronizationFailedException {
		Mod[] prims = createPrimitiveModList(3, null);
		Object[] values = new Object[] {1, 2};
		final Mod[] result = checkedResynchronize(_sync, values, prims);
		assertArrayEquals(createPrimitiveModList(3, 3), result);
	}

	@Test(expected=SynchronizationFailedException.class)
	public void testConflictMod() throws SynchronizationFailedException {
		Mod[] prims = createPrimitiveModList(3, 4);
		Object[] values = new Object[] {1, 2};
		checkedResynchronize(_sync, values, prims);
	}

	
	
}
